{{if false}}<script>{{/if}}
{{$controller = $smarty.const.CCTRL}}
{{$token = $smarty.const.TOKEN}}
{{$display=0}}
includecss("{{base_url()}}syslib/jqwidgets/styles/jqx.base.css");
includecss("{{base_url()}}syslib/jqwidgets/styles/jqx.classic.css");
includejs("{{base_url()}}syslib/jqwidgets/globalization/jquery.global.js");
includejs("{{base_url()}}syslib/jqwidgets/jqx-all.js");
//include("{{base_url()}}syslib/jqwidgets/gettheme.js");
{{include file="../00_base/colmod_advanced.tpl"}}
var myjqxGrid;
var myGrid=$("#jqxGrid");
$(document).ready(function(){
    initSetting();
    myjqxGrid= (function () {
        var theme = 'classic';
        var source ={
            datatype: "json",
            type        :   "POST",
            datafields  : adatafiels,
            url         : '{{base_url()}}backend/{{$controller}}/binding/{{if isset($type)}}{{$type}}{{/if}}',
            filter      : function(){ 
                myGrid.jqxGrid('updatebounddata', 'filter');
                
            },
            sort        : function(){ 
                myGrid.jqxGrid('updatebounddata');
                
            },
            root        : 'Rows',
            beforeprocessing: function(data){		
                    source.totalrecords = data[0].TotalRows;					
            }
        };	
        var dataadapter = new $.jqx.dataAdapter(source, {
            loadError: function(xhr, status, error){
                tipMessage("<b>Status</b>:"+xhr.status+"<br/><b>ThrownError</b>:"+error+"<br/>");
            }
        });
        if(typeof handleEvents == 'function') { 
            handleEvents();
        }
        var gridconfig={
            width:'100%',
            //autoheight: true,
            //rowsheight: 24,
            source: dataadapter,
            theme: theme,
            sortable: true,
            ready: function () {

            },
            rendergridrows: function(obj)
            {
                return obj.data;    
            },
            //groupable:true,
            filterable: true,
            autoshowfiltericon: true,
            showfilterrow: true,
            editable:true,
            editmode:'dblclick',
            selectionmode:'multiplecellsextended',
            height:($(document).height() - 200)>500?($(document).height() - 200):500,
            virtualmode: true,
            columns: acolumns
        };
        gridconfig.pageable =    true;
        gridconfig.pagesize =    100;
        gridconfig.pagesizeoptions  =   ['50','100', '200', '500' , '1000'];
        gridconfig.pageable=true;

        //Adding event listeners
        function _addEventListeners() {

        };
        //Creating all page elements which are jqxWidgets
        function _createElements() {

        };
        //Creating the demo window
        function _createWindow() {
    //        $("#window-sOpt").jqxWindow({
    //            autoOpen: false,showCollapseButton: true, maxHeight: 400, maxWidth: 700, 
    //            minHeight: 100, minWidth: 200, width: 420, theme: theme 
    //        });
        };
        function _createGrid(){
            try{
                myGrid.jqxGrid(gridconfig);
                //handleEvent_jqxGrid(myGrid);
            }catch(e){ tipMessage(e.message); }
        }
        return {
            Init:function(){
                _createGrid();
                
            },
            Setting:function(){
                
            },
            Refresh:function(){
                myGrid.jqxGrid('updatebounddata');
                
            },
            ShowColumn:function(col){
                console.log("Hide column: "+col+" ↵ Call");
                myGrid.jqxGrid('hidecolumn', col);
                myjqxGrid.DisplayColumnChange(col,1);
            },
            HideColumn:function(col){
                console.log("Show column: "+col+" ↵ Call");
                myGrid.jqxGrid('showcolumn', col);
                myjqxGrid.DisplayColumnChange(col,0);
            },
            AddItem:function(){
                try{
                    console.log("AddItem ↵ Call");
                    var url,data,obj,id;
                    url="{{base_url()}}backend/{{$controller}}/edit";
                    data={ }
                    obj=$(".frmeditting");
                    isrunning=true;
                    obj.html("<div style='display:block;margin:0 auto;width:60px;height:60px' class='ajaxloadding'></div>");
                    htmlAjax(url,data,obj,function(){
                        {{if isset($isPopup)}}
                            ShowConfirmDialogMessage(obj, "New record.", SaveItem);
                        {{else}}
                            $(".frmjqxgrid").hide();
                            $(".frmeditting").show();
                            $(".group-events.g").hide();
                            $(".group-events.e").show();
                        {{/if}}
                    });

                }catch(e){
                    console.log("JSON Error:"+e.message+"\n↵ Error");
                    ShowErrorDialogMessage(e.message);
                    //ShowErrorDialogMessage("Sorry. Your request could not be completed.<br/> Please check your input data.");
                }
            },
            EditItem:function(){
                try{
                    var cells = myGrid.jqxGrid('getselectedcells');
                    var _id=undefined,url,data,obj;
                    if(cells.length>0){
                        for(var i=0;i<cells.length;i++){
                            if(cells[i].rowindex!=undefined){
                                var _dataRow=myGrid.jqxGrid('getrowdata', cells[i].rowindex);
                                _id=_dataRow._id==undefined?(_dataRow.ID==undefined?_dataRow.ProductID:_dataRow.ID):_dataRow._id;
                                break;
                            }
                        }
                        if(_id!=undefined){
                            console.log("EditItem:"+_id+" ↵ Call");
                            url="{{base_url()}}backend/{{$controller}}/edit/"+_id;
                            data={ }
                            obj=$(".frmeditting");
                            if(_id != $("#_id").val()){
                                isrunning=true;
                                obj.html("<div style='display:block;min-width:60px;height:60px' class='ajaxloadding'></div>");
                                htmlAjax(url,data,obj,function(){
                                    {{if isset($isPopup)}}
                                        ShowConfirmDialogMessage(obj, "Edit record", function(){SaveItem();});
                                    {{else}}
                                        $(".frmjqxgrid").hide();
                                        $(".frmeditting").show();
                                        $(".group-events.g").hide();
                                        $(".group-events.e").show();
                                    {{/if}}
                                });
                            }else{
                                {{if isset($isPopup)}}
                                    ShowConfirmDialogMessage(obj, "Edit record", function(){SaveItem();});
                                {{else}}
                                    $(".frmjqxgrid").hide();
                                    $(".frmeditting").show();
                                    $(".group-events.g").hide();
                                    $(".group-events.e").show();
                                {{/if}}
                            }
                        }else{
                            uiMessage("No row selected. Please select one or more rows.");
                        }
                    }else{
                        uiMessage("No row selected. Please select one or more rows.");
                    }
                }catch(e){
                    console.log("JSON Error:"+e.message+"\n↵ Error");
                    ShowErrorDialogMessage(e.message);
                    //ShowErrorDialogMessage("Sorry. Your request could not be completed.<br/> Please check your input data.");
                }
            },
            CancelEdit:function(){
                console.log("CancelEdit ↵ Call");
                {{if isset($isPopup)}}
                    var confirm=$(".frmeditting");
                    CloseConfirmDialogMessage(confirm);
                {{else}}
                    $(".frmjqxgrid").show();
                    $(".frmeditting").hide();
                    $(".group-events.g").show();
                    $(".group-events.e").hide();
                {{/if}}
            }
        }
    } ());
    myjqxGrid.Init();
    //console.log(acolumns);
});
function gridSetting(){
    var tit="<img class='pa t7 l8 ico16'src='{{base_url()}}syslib/sysimages/16/option.gif'/><font class='pl20'>Settings !</font>";
    ShowDialogMessage($("#window-sOpt"),tit);
}
function gridRefresh(){
    myjqxGrid.Refresh();
}
function toogleEvent(){
    $(".btnseparator.hidden,.events.hidden").toggle();
    $(".events span.show").toggleClass("hide");
}